/* FIGURES 3 & 4 IN COSAR, GRIECO AND TINTELNOT (2014) 
Inputs: 
- ModelOutcomes.txt, prepared by extract_data_for_graphs.m
Outputs: 
- figure3.eps
- figure3.png
- figure4.eps
- figure4.png
*/

clear
global path "C:\Users\Felix Tintelnot\Dropbox\WindBorder\RestatCGTReplication"

// Load data, rename
insheet using "$path\SummaryStatistics\ModelOutcomes.txt" 
rename v1 distance_border
rename v2 country      // 1 if project is in German 
rename v3 prodc_denmark // 1 if project supplied by a Danish firm
rename v4 den_baseline // baseline model predicted winning probabilities
rename v5 den_nf    // counterfactual winning probabilities under no fixed costs
rename v6 den_nb   // counterfactual winning probabilities with no border costs
replace distance_border = distance_border*100
drop v7
replace distance_border=-distance_border if country==0
sort country distance_border

// Polynomial terms
gen distance_border2=distance_border^2
gen distance_border3=distance_border^3
gen interact = distance_border*country
gen interact2 = distance_border2*country
gen interact3 = distance_border3*country

// RDD regression
regress prodc_denmark distance_border* interact* country, robust level(99)  
predictnl yhat=predict(), ci(yhat_lc yhat_uc) 

drop if yhat>1

gen yhat_den = yhat if country==0
gen yhat_ger = yhat if country==1

save temp.dta,replace
clear
use temp

// Local averages in Denmark
keep if country==0
xtile bins = distance_border, nq(4)
sort bins
by bins: egen localdistance=mean(distance_border)
by bins: egen localnumprojects=count(distance_border)
by bins: egen localdanish=sum(prodc_denmark)
gen localdanishmarketshare = localdanish/localnumprojects
duplicates drop localdanishmarketshare localdistance,force
rename localdanishmarketshare localdanishmarketshare_dnk
rename localdistance localdistance_dnk
keep localdanishmarketshare_dnk localdistance_dnk

append using temp
save temp2.dta,replace
clear

// Local averages in Germany
use temp
keep if country==1
xtile bins = distance_border, nq(8)
sort bins
by bins: egen localdistance=mean(distance_border)
by bins: egen localnumprojects=count(distance_border)
by bins: egen localdanish=sum(prodc_denmark)
gen localdanishmarketshare = localdanish/localnumprojects
duplicates drop localdanishmarketshare localdistance,force
rename localdanishmarketshare localdanishmarketshare_ger
rename localdistance localdistance_ger
keep localdanishmarketshare_ger localdistance_ger

append using temp2

erase temp.dta
erase temp2.dta

// Model fit
regress den_baseline distance_border* interact* country, level(95)
predict yhat_baseline
gen yhat_base_den = yhat_baseline if country==0
gen yhat_base_ger = yhat_baseline if country==1

// No fixed cost fit
regress den_nf distance_border* interact* country, robust
predict yhat_nf
gen yhat_nf_den = yhat_nf if country==0
gen yhat_nf_ger = yhat_nf if country==1

// No border fit
regress den_nb distance_border* interact* country, robust
predict yhat_nb
gen yhat_nb_den = yhat_nb if country==0
gen yhat_nb_ger = yhat_nb if country==1

/**/
label variable yhat_base_den "Baseline model"
label variable yhat_den "Data"

drop if distance_border<-300
drop if distance_border>675

// Model fit: figure 3
twoway (line yhat_base_den yhat_base_ger distance_border, xline(0) lpattern(dash dash) lcolor(blue blue) lwidth(thick thick)) ///
(line yhat_den yhat_ger distance_border, lcolor(maroon maroon) lwidth(thick thick)), ///
ytitle(Market share, margin(medium)) yscale(range(0 1)) ylabel(#5) xtitle(Distance to border (km)) ///
xlabel(-300(75)675 0, angle(vertical)) note((Distance is negative for Denmark, positive for Germany), ///
size(vsmall) position(6)) legend(row(2) order(1 3) ring(0) pos(2)) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "$path\SummaryStatistics\figure3.eps", replace
graph export "$path\SummaryStatistics\figure3.png", replace

// Counterfactuals: figure 4
label variable yhat_base_den "Baseline model"
label variable yhat_nf_den "No fixed costs"
label variable yhat_nb_den "No national border"
label variable yhat_den "Data"

twoway (line yhat_base_den yhat_base_ger distance_border, xline(0) lpattern(dash dash) lcolor(blue blue) lwidth(thick thick)) ///
(line yhat_nf_den yhat_nf_ger distance_border, lcolor(magenta magenta) lwidth(thick thick) lpattern(shortdash_dot shortdash_dot)) ///
(line yhat_nb_den yhat_nb_ger distance_border, lcolor(green green) lwidth(thick thick) lpattern(longdash_dot longdash_dot)), ///
ytitle(Market share, margin(medium)) yscale(range(0 1)) ylabel(#5) xtitle(Distance to border (km)) ///
xlabel(-300(75)675 0, angle(vertical)) note((Distance is negative for Denmark, positive for Germany), ///
size(vsmall) position(6)) legend(row(4) order(1 3 5) ring(0) pos(2)) plotregion(fcolor(white)) graphregion(fcolor(white))

graph export "$path\SummaryStatistics\figure4.eps", replace
graph export "$path\SummaryStatistics\figure4.png", replace
